<?php

/**
 * This is the model class for table "{{user_balance}}".
 *
 * The followings are the available columns in table '{{user_balance}}':
 * @property integer $ID
 * @property integer $userID
 * @property integer $balance
 * @property integer $promotion
 */
class UserBalance extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return UserBalance the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user_balance}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ID, userID, balance, promotion', 'required'),
			array('ID, userID, balance, promotion', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ID, userID, balance, promotion', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ID' => 'ID',
			'userID' => 'User',
			'balance' => 'Tài khoản chính',
			'promotion' => 'Tài khoản khuyến mãi',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ID',$this->ID);
		$criteria->compare('userID',$this->userID);
		$criteria->compare('balance',$this->balance);
		$criteria->compare('promotion',$this->promotion);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	//function getBalance
	public function getBalance($userID) {
		$balance = $this->model()->findByAttributes(array('userID'=>$userID));           
    if (!empty($balance))
    {
      return $balance['balance'];	       
    }
    else
    {
    	return 0;
    }
	}
	/**
	 * get money promotion
	 * @author K.D
	 */
  public function getPromotion($userID) {
		$balance = $this->model()->findByAttributes(array('userID'=>$userID));           
    if (!empty($balance))
    {
      return $balance['promotion'];	       
    }
    else
    {
    	return 0;
    }
	}
	
	/**
	 * add new UserPayment record.	 *
	 * @param int $userID
	 * @param int $status
	 * @param double $money
	 */
	public function addUserPayment($userID,$status,$money) {
		    
		    $userpayment = new UserPayment();
		    //$model->money = 10000;		   		   
		    $userpayment->userID = $userID;
		    $userpayment->money = $money;		   
		    $userpayment->datetime = time();
				$userpayment->status = $status; // out-balance

				if($status==1)
		    {
		    	 $userpayment->description = 'Nạp tiền '.$money.' VND';
		    }
		    else if($status==2)
		    {
		    	 $userpayment->description = 'Tiền giải bài tập (trích tài khoản chính)'.$money.' VND';
		    }
		    else if($status==3)
		    {
		    	 $userpayment->description = 'Khuyến mãi '.$money.' VND';
		    }
		    else if($status==4)
		    {
		    	 $userpayment->description = 'Tiền giải bài tập ( trích từ tài khoản KM)'.$money.' VND';
		    }	 		
		    
		    $userpayment->save(false);	
	}
	
	/**
	 * Update UserBalance
	 * @param unknown_type $userID
	 * @param unknown_type $type
	 * @param unknown_type $money
	 */
	public function updateUserBalance($userID,$type,$money) {
		//Trừ tiền trong UserBalance và ghi log userpayment
		$userbalance = UserBalance::model()->findByAttributes(array('userID'=>$userID));    
	  if(empty($userbalance))
	  {		
			$userbalance = new UserBalance();
			$userbalance->userID = $userID;
		}
		
		if(type==1)
		{
			$userbalance->balance = $userbalance->balance + $money;			
		}
		else if(type==2)
		{
			$userbalance->balance = $userbalance->balance - $money;			
		}
		else if(type==3)
		{
			$userbalance->promotion = $userbalance->promotion + $money;			
		}
		else if(type==4)
		{
			$userbalance->promotion = $userbalance->promotion - $money;			
		}
		else
		{
			
		}			
		
		$userbalance->save(false);
	}

	/**
	 * get balance or promotion
	 * @author K.D
	 */
  public function checkBalance($userID,$in,$out) {
  	$in = Yii::app()->db->createCommand()
  						->select('sum(up.money) as sum')
  						->from('tbl_user_payment as up')
  						->where('up.status = '.$in.' AND userID='.$userID)
  						->queryRow();
  	$out = Yii::app()->db->createCommand()
  						->select('sum(up.money) as sum')
  						->from('tbl_user_payment as up')
  						->where('up.status = '.$out.' AND userID='.$userID)
  						->queryRow();
  	if(empty($in)) {
  		$sum_in = 0;
  	}
  	else {
  		$sum_in = $in['sum'];
  	}
  	if(empty($out)) {
  		$sum_out = 0;
  	}
  	else {
  		$sum_out = $out['sum'];
  	}
  	$sum = $sum_in - $sum_out;
	return $sum;
}
}